package com.epalmpay.service.datareport.impl;

import com.epalmpay.dto.datareport.ManagerWelcomeDataOutDTO;
import com.epalmpay.entity.StatisEpalmpay;
import com.epalmpay.entity.StatisEpalmpayDay;
import com.epalmpay.entity.StatisEpalmpayMonth;
import com.epalmpay.mapper.StatisEpalmpayDayMapper;
import com.epalmpay.mapper.StatisEpalmpayMapper;
import com.epalmpay.mapper.StatisEpalmpayMonthMapper;
import com.epalmpay.mapper.SysMessageInfoMapper;
import com.epalmpay.service.datareport.IManagerStatisticService;
import com.epalmpay.util.DateUtil;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

/**
 * Created by lifayi on 2017/6/14.
 */
@Service
public class ManagerStatisticServiceImpl implements IManagerStatisticService {

    @Autowired
    private StatisEpalmpayMapper statisEpalmpayMapper;
    @Autowired
    private SysMessageInfoMapper sysMessageInfoMapper;
    @Autowired
    private StatisEpalmpayMonthMapper statisEpalmpayMonthMapper;
    @Autowired
    private StatisEpalmpayDayMapper statisEpalmpayDayMapper;

    @Override
    public ManagerWelcomeDataOutDTO index(Long id) {
        ManagerWelcomeDataOutDTO managerWelcomeDataOutDTO = new ManagerWelcomeDataOutDTO();
        Long lastId = null;
        Map result = statisEpalmpayMapper.selectLastId();
        if(result != null) {
            lastId = (Long) result.get("lastId");
        }

        if(lastId != null) {
            StatisEpalmpay statisEpalmpay = statisEpalmpayMapper.selectByPrimaryKey(lastId);
            if(statisEpalmpay != null) {
                managerWelcomeDataOutDTO.setDeskcardNum(statisEpalmpay.getDeskcardNum());
                managerWelcomeDataOutDTO.setPosNum(statisEpalmpay.getPosNum());
                managerWelcomeDataOutDTO.setProfitSumAmount(statisEpalmpay.getProfitSumAmount());
                managerWelcomeDataOutDTO.setTradeSumAmount(statisEpalmpay.getTradeSumAmount());
                managerWelcomeDataOutDTO.setGroupNum(statisEpalmpay.getGroupNum());
                managerWelcomeDataOutDTO.setShopNum(statisEpalmpay.getShopNum());
            }
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
        String month = simpleDateFormat.format(DateUtil.lastMonthFirstDate());

        StatisEpalmpayMonth statisEpalmpayMonth = statisEpalmpayMonthMapper.selectByStatisDate(month);
        if(statisEpalmpayMonth != null) {
            managerWelcomeDataOutDTO.setThisGroupNum(statisEpalmpayMonth.getAddGroupNum());
            managerWelcomeDataOutDTO.setThisShopNum(statisEpalmpayMonth.getAddShopNum());
        }

        Date date = DateUtils.addDays(DateUtil.getDayStart(new Date()), -1);

        StatisEpalmpayDay statisEpalmpayDay = statisEpalmpayDayMapper.selectByStatisDate(date);
        if(statisEpalmpayDay != null) {
            managerWelcomeDataOutDTO.setYestTradeSumAmount(statisEpalmpayDay.getTradeSumAmount());
            managerWelcomeDataOutDTO.setYestProfitSumAmount(statisEpalmpayDay.getProfitSumAmount());
        }

        Integer undisposedMessageNum = sysMessageInfoMapper.countUserUndisposedMessage(id);
        managerWelcomeDataOutDTO.setUndisposedMessageNum(undisposedMessageNum);

        return managerWelcomeDataOutDTO;
    }
}
